package com.liyuci.core.thymeleaf.eleondialect;

import com.liyuci.core.helper.BlankHelper;

import java.util.ArrayList;
import java.util.List;

import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IModel;
import org.thymeleaf.model.IModelFactory;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractElementTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.templatemode.TemplateMode;

/**
 * thymeleaf自定义标签基类
 * @author liyuci
 * @date 2018-02-05 22:53:13
 */

public abstract class BaseElementTagProcessor extends AbstractElementTagProcessor {

	// 优先级
	public static final int PRECEDENCE = 10000;
	
	/**
	 * 
	 * @param templateMode
	 * @param dialectPrefix
	 * @param elementName
	 * @param prefixElementName
	 * @param attributeName
	 * @param prefixAttributeName
	 * @param precedence
	 */
	public BaseElementTagProcessor(TemplateMode templateMode, String dialectPrefix, String elementName, boolean prefixElementName, String attributeName, boolean prefixAttributeName, int precedence)
	{
		super(templateMode, dialectPrefix, elementName, prefixElementName, attributeName, prefixAttributeName, precedence);
	}
	
	/**
	 * 获取标签属性的真实值
	 * @author liyuci
	 * @date 2018-02-06 08:50:52
	 * @param context
	 * @param tag
	 * @param attributeName
	 * @param defaultValue
	 * @return
	 */
	protected String getString(ITemplateContext context, IProcessableElementTag tag, 
			String attributeName, String defaultValue)
	{
		String attributeValue = getString(context, tag, attributeName);
		//真实值为空就返回不为空的默认值
		if(BlankHelper.isBlank(attributeValue) && BlankHelper.isNotBlank(defaultValue))
		{
			attributeValue = defaultValue;
		}
		return attributeValue;
	}
	
	/**
	 * 获取标签属性的真实值
	 * @author liyuci
	 * @date 2018-02-05 23:03:04
	 * @param context
	 * @param tag
	 * @param attributeName
	 * @return
	 * @throws Exception
	 */
	protected String getString(ITemplateContext context, IProcessableElementTag tag, 
			String attributeName)
	{
		String attributeValue = "";
		//获取原始值（也有可能是表达式）
		String rawValue = ThymeleafFacade.getRawValue(tag, attributeName);
		if(BlankHelper.isNotBlank(rawValue))
		{
			//根据原始值获取其对应的真实值
			Object obj = ThymeleafFacade.evaluateRawValue(context, rawValue);
			if(BlankHelper.isNotBlank(obj))
			{
				attributeValue = obj.toString();
			}
		}
		return attributeValue;
	}
	
	@SuppressWarnings("rawtypes")
	protected List getList(ITemplateContext context, IProcessableElementTag tag, 
			String attributeName)
	{
		//获取原始值（也有可能是表达式）
		String rawValue = ThymeleafFacade.getRawValue(tag, attributeName);
		if(BlankHelper.isNotBlank(rawValue))
		{
			//根据原始值获取其对应的真实值
			return ThymeleafFacade.evaluateAsList(context, rawValue);
		}
		else
		{
			return new ArrayList<>();
		}
	}
	
	protected List<String> getListByDelimiter(ITemplateContext context, IProcessableElementTag tag, 
			String attributeName)
	{
		//获取原始值（也有可能是表达式）
		String rawValue = ThymeleafFacade.getRawValue(tag, attributeName);
		if(BlankHelper.isNotBlank(rawValue))
		{
			//根据原始值获取其对应的真实值
			return ThymeleafFacade.evaluateAsListStringWithDelimiter(context, rawValue, ",");
		}
		else
		{
			return new ArrayList<>();
		}
	}
	
	/**
	 * 生成html元素信息
	 * @author liyuci
	 * @date 2018-02-06 22:19:30
	 * @param context
	 * @param structureHandler
	 * @param html
	 */
	public void generateHtml(ITemplateContext context, IElementTagStructureHandler structureHandler, StringBuilder html)
	{
		final IModelFactory modelFactory = context.getModelFactory();
		final IModel model = modelFactory.createModel();
		model.add(modelFactory.createText(html.toString()));
		/*
		 * 指示引擎用指定的模型替换整个元素。
		 */
		structureHandler.replaceWith(model, false);
	}
	
	/**
	 * 将id和name转为html元素信息
	 * @author liyuci
	 * @date 2018-02-06 22:32:04
	 * @param id
	 * @param name
	 * @return
	 */
	public String getIdAndNameHtml(String id, String name)
	{
		StringBuilder html = new StringBuilder();
		if(BlankHelper.isNotBlank(id))
		{
			html.append(" id='"+ id +"' ");
		}
		if(BlankHelper.isNotBlank(name))
		{
			html.append(" name='"+ name +"' ");
			if(BlankHelper.isBlank(id))
			{
				html.append(" id='"+ name +"' ");
			}
		}
		return html.toString();
	}
}
